<?php

namespace app\adminapi\controller;

use think\Controller;
use think\facade\Request;
use app\adminapi\model\Admin as admintable;
use think\Db;

class Admin extends BaseApi
{
    //
    // 获取管理员信息
    public function index()
    {
        // $this->ok('sajka');
        // 接受参数
        $param = Request::param();
        // $where=[];
        $pageSize = $param['pageSize'];
        // 测试
        // 查询数据
        // $this->ok($param);
        // 按条件搜索
        if (!empty($param['keyword'])) {
            $keyword = $param['keyword'];
            // $data = admintable::where('username','like',"%$keyword%")->paginate(2);
            // $where=['like'."%$keyword%"];
            $data = admintable::alias('t1')->join('pyg_role t2', 't1.`role_id`=t2.`id`', 'left')
                ->field('t1.*,t2.role_name')->where('username', 'like', "%$keyword%")->paginate($pageSize);
        } else {
            // $data = admintable::paginate(2);
            $data = admintable::alias('t1')->join('pyg_role t2', 't1.`role_id`=t2.`id`', 'left')
                ->field('t1.*,t2.role_name')->paginate($pageSize);
        }
        // $this->ok($data,200,'查到0条数据');
        $this->ok($data);
    }
    //修改用户信息
    public function edit()
    {
        //获取数据
        $param = Request::param();
        // 测试数据
        // dump($param);
        // $this->ok($param);
        // 验证
        // 验证规则
        $rule = [
            'email|邮箱' => 'email',
            'nickname|昵称' => 'max:50',
        ];
        // 错误信息
        $msg = [
            'email' => '邮箱格式不正确',
            'nickname' => '昵称不能超过50',
        ];
        $validate = new \think\Validate($rule, $msg);

        if (!$validate->check($param)) {
            $err = $validate->getError();
            $this->fail($err);
        }
        // 修改数据
        admintable::where('id', $param['id'])->update(['email' => $param['email'], 'nickname' => $param['nickname']]);
        $info=admintable::find($param['id']);
        // 返回数据
          $this->ok($info);  
    }

    public function delect(){
        $param = Request::param();
        // dump($param);
    }
}